from django.shortcuts import render
from rest_framework.views import APIView
from .models import *
from .ser import *
from rest_framework.response import Response
from django.core.paginator import Paginator


# Create your views here.

class AshopAPI(APIView):
    def get(self, request):
        cpage = request.GET.get("cpage")
        ashop = Ashop.objects.all()
        p = Paginator(ashop, 2)
        allshop = p.page(cpage)
        ser = AshopSer(allshop, many=True)
        return Response({"code": 200, "list": ser.data, "numm": p.num_pages})

    def post(self, request):
        username = request.data.get('username')
        password = request.data.get('password')
        user = Users.objects.filter(username=username).first()
        if user:
            if user.password == password:
                return Response({"code": 200, "userid": user.id})
        return Response({"code": 20010, "mes": "错误"})

class AspAPI(APIView):
    def post(self,request):
        ser = AshopSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code":200})
        else:
            print(ser.errors)
            return Response({"code":10010})